<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="z" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%
	com.test.model.User currentUser = (com.test.model.User) org.apache.shiro.SecurityUtils.getSubject().getPrincipal();
	request.setAttribute("currentUser", currentUser);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/head.jsp" />
<script type="text/javascript">
$(function () {
    $("#queryForm").bootstrapValidator({
        message:'This value is not valid',
        feedbackIcons: {
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            personName:{
                message:'姓名非法',
                validators:{
                    regexp:{
                        regexp:/^[\u4e00-\u9fa5]{2,10}$/,
                        message:'姓名由汉字组成'
                    }
                }
            },
            identityNumber:{
                message:'身份证号码非法',
                validators:{
                    stringLength:{
                        min:18,
                        max:18,
                        message:'身份证号码长度必须为18位'
                    },
                    regexp:{
                    	regexp:/^[a-zA-Z0-9]+$/,
                        message:'身份证号码由数字字母组成'
                    }
                }
            },
        	
            birthTime:{
                message:'出生年份非法',
                validators:{
                    stringLength:{
                        min:4,
                        max:4,
                        message:'出生年份长度必须为4位'
                    },
                    regexp:{
                        regexp:/^[0-9]{4,4}$/,
                        message:'出生年份由数字组成'
                    }
                }
            },

        }
    })
})

	function validate(){
		var id = $("#identityNumber").val(); 
		var personName = $("#personName").val(); 
		var sex = $("select[name='sex'] option:selected") .val();
		var birthTime = $("#birthTime").val(); 
		if(id==""){
			if((personName==""&&sex=="")||(personName==""&&birthTime=="")||(birthTime==""&&sex=="")||(personName==""&&birthTime==""&&sex=="")) {
				window.location.href="error.jsp";
				return false;
			}else { 
				return true;
			} 
		}else{
			return true;
		}
	}

</script>
<style type="text/css">
body {
	padding: 40px;
	position: relative;
}

h3 {
	text-align: center;
}

table, th, td {
	text-align: center;
}

.div {
	margin: 0 auto;
	width: 250px;
}
</style>
</head>
<body>
	<nav class="navbar navbar-default navbar-static navbar-fixed-top" role="navigation">
	<div class="container-fluid">
		<div>
			<ul class="nav navbar-nav navbar-right">
				<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">欢迎您，${currentUser.realName}
						<span class="caret"></span>
				</a>
					<ul class="dropdown-menu">
						<li><a href="updatePassword.jsp" onclick="window.open(this.href);return false"> 更改个人密码 </a></li>
						<shiro:hasPermission name="USER_REGISTER">
							<li><a href="register.jsp" onclick="window.open(this.href);return false"> 注册新用户 </a></li>
						</shiro:hasPermission>
						<li><a href="/test/logout.do">退出登录</a></li>
					</ul></li>
			</ul>
		</div>
	</div>
	</nav>
	<div class="container col-md-8 col-md-offset-2">
		<div class="page-header">
			<h3>人员查询</h3>
		</div>
		<div>

			<form action="/test/getPersonList.do?page=0&size=10" method="post" id="queryForm" class="form-horizontal" role="form"
				onsubmit="return validate()">
				<input type="hidden" id="department" name="department" value="${currentUser.department}" />
				<div class="form-group">
					<label for="identityNumber" class="col-md-2 control-label">身份证号码</label>
					<div class="col-md-5">
						<input type="text" class="form-control" id="identityNumber" name="identityNumber" placeholder="请输入身份证号码"
							value="${identityNumber}">
					</div>
				</div>

				<div class="form-group">
					<label for="personName" class="col-md-2 control-label">姓名</label>
					<div class="col-md-5">
						<input type="text" class="form-control" id="personName" name="personName" placeholder="请输入姓名"
							value="${personName}">
					</div>
				</div>

				<div class="form-group">
					<label for="sex" class="col-md-2 control-label">性别</label>
					<div class="col-md-5 ">
						<z:codeSelector entityName="Gender" name="sex" value="${sex}" />
					</div>
				</div>

				<div class="form-group">
					<label for="birthTime" class="col-md-2 control-label">出生年份</label>
					<div class="col-md-5">
						<input type="text" class="form-control" id="birthTime" name="birthTime" placeholder="请输入出生年份" value="${birthTime}">
					</div>
					<button type="submit" class="btn btn-primary">查询</button>
					<shiro:hasPermission name="USER_UPDATE">
						<a href="increasePerson.jsp" type="button" onclick="window.open(this.href);return false" class="btn btn-default">新增人员信息</a>
					</shiro:hasPermission>
				</div>
			</form>
			<hr>
		</div>

		<c:choose>
			<c:when test="${personlist.size() >0}">
				<table class="table table-striped table-bordered  table-hover">
					<thead>
						<tr>
							<th>序号</th>
							<th>姓名</th>
							<th>性别</th>
							<th>出生日期</th>
							<th>所在部门</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<c:forEach var="personlist" items="${personlist}" varStatus="status">
							<tr>
								<td>${ status.index + 1}</td>
								<td>${personlist.personName}</td>
								<td>
									<z:codeName entityName="Gender" value="${personlist.gender}" />
								</td>
								<td>${personlist.birthTime.toString().substring(0, 10)}</td>
								<td>
									<z:codeName entityName="Department" value="${personlist.department}" />
								</td>
								<td>
									<a href="/test/seePerson.do?id=${personlist.id}" type="button" onclick="window.open(this.href);return false"
										class="btn btn-primary">查看</a>

									<shiro:hasPermission name="USER_UPDATE">
										<a href="/test/updatePerson.do?id=${personlist.id}" type="button"
											onclick="window.open(this.href);return false" class="btn btn-warning">修改</a>
									</shiro:hasPermission>

									<shiro:hasPermission name="USER_DELETE">
										<button type="button" class="btn btn-danger delete" value="${personlist.id}">删除</button>
									</shiro:hasPermission>
								</td>
							</tr>
						</c:forEach>
					</tbody>
				</table>

				<div>
					<ul class="pagination" id="pagination">
						<li id="previous"><a
							href="/test/getPersonList.do?page=${page-1}&size=${size}&personName=${personName}&sex=${sex}&birthTime=${birthTime}&identityNumber=${identityNumber}">&laquo;</a></li>
						<c:forEach var="i" begin="1" end="${pageAmount}" step="1">
							<li><a
								href="/test/getPersonList.do?page=${i-1}&size=${size}&personName=${personName}&sex=${sex}&birthTime=${birthTime}&identityNumber=${identityNumber}">
									${i} </a></li>
						</c:forEach>
						<li id="next"><a
							href="/test/getPersonList.do?page=${page+1}&size=${size}&personName=${personName}&sex=${sex}&birthTime=${birthTime}&identityNumber=${identityNumber}">&raquo;</a></li>
					</ul>
				</div>

				<div class="div">
					<p class="form-control-static">共${pageAmount}页，共有${element}条记录，当前第${page+1}页</p>
				</div>

			</c:when>
			<c:otherwise>
			</c:otherwise>
		</c:choose>

	</div>
	<script>
	
	$(".delete").click(function(){
		 var ID = $(this).val();//获取id属性
	        $.ajax({  
	            type : "POST",  //提交方式  
	            url : "/test/deletePerson.do",//路径  
	            data : {   "id" : ID   },//数据，这里使用的是Json格式进行传输  
	            success : function() {//返回数据根据结果进行相应的处理  
	            	alert("删除成功");
            		window.location.reload(); 
            		if("${page}">0&&"${personlist.size()}"==1){
            			window.location.href="/test/getPersonList.do?page=${page-1}&size=${size}&personName=${personName}&sex=${sex}&birthTime=${birthTime}&identityNumber=${identityNumber}";
            		}
	            }  ,
	            error : function(){
	            	alert("删除失败")
	            }
	        });  
	});
	
	if("${page}"<1){
		$("#previous").hide();
	}
	if("${page}">"${pageAmount-2}" ){
		$("#next").hide();
	}
	if("${pageAmount}"<=1){
		$("#pagination").hide();
	}
	 
  	</script>
</body>
</html>